﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using HySoft.Cash;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using HySoft.Cash.BaseClass;
using HySoft.Cash.Structure.HySoft.Cash.Structure;
using PayRoller.Code;

namespace PayRoller.Include
{
    public partial class UStatementDetail : System.Web.UI.UserControl
    {
        protected List<TStatementInvoiceDT> StatementInvoiceList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            string statementGuid = this.Request.QueryString["Guid"];

            //if (this.IsPostBack == false&& string.IsNullOrEmpty( statementGuid)==false)
            if (string.IsNullOrEmpty(statementGuid) == false)
            {
                //this.LoadBasicInfo();
                int currentOrgID = this.LoadStatementInfo(statementGuid);
                if (currentOrgID > 0)
                {
                    LoadOrgInfo(currentOrgID);
                }

                StatementInvoiceList = this.GetStatementInvoices(statementGuid);
                this.DisplayAvalableBalance();
            }
        }

        //private void LoadBasicInfo()
        //{
        //    //this.labAnnualRate.Text = Convert.ToString(this.GetSysOptionValue(4));
        //    //this.labDailyRate.Text = Convert.ToString(this.GetSysOptionValue(5));
        //}

        private int LoadStatementInfo(string statementGuid)
        {
            using (TStatementBN statementBN = new TStatementBN(this.Page))
            {
                TStatementDT statementDT = statementBN.Get(new Guid(statementGuid));
                int currentOrgID = statementDT.OrgID;

                this.labStatementDate.Text = statementDT.StatementDate.ToString(AppSettings.DateFormatAU, AppSettings.CurrentCulture);
                this.labOrgID.Text = currentOrgID.ToString();
                this.labStatementID.Text = statementDT.ID.ToString();
                this.labTotalInvoiceAmount.Text = (statementDT.TotalInterest + statementDT.TotalPrincipal).ToString("0.00");
                this.labTotalLateInterestCharges.Text = statementDT.TotalLateInterestCharges.ToString("0.00"); ;
                this.labTotalPenalty.Text = statementDT.TotalPenalty.ToString("0.00");

                this.labClosingBalance.Text = statementDT.LastCreditBalance.ToString("0.00");
                //this.labClosingBalance2.Text = orgDT.CreditBalance.ToString("0.00");

                this.labTotalPayable.Text = statementDT.LastFeeBalance.ToString("0.00");

                return currentOrgID;
            }
        }

        private void LoadOrgInfo(int currentOrgID)
        {
            using (TorginfoBN orgBN = new TorginfoBN(this.Page))
            {
                TorginfoDT orgDT = orgBN.Get(currentOrgID);
                this.labAddress.Text = orgDT.ABN;
                this.labContactPerson.Text = orgDT.ContactPerson;
                this.labCreditLimit.Text = orgDT.CreditLimit.ToString("0.00");
                this.labOrgName.Text = orgDT.OrgName;
            }
        }

        private void DisplayAvalableBalance()
        {
            decimal orgCreditLimit = 0M;
            if (this.labCreditLimit.Text != string.Empty)
            {
                decimal.TryParse(this.labCreditLimit.Text, out orgCreditLimit);
            }

            decimal closingBalance = 0M;
            if (this.labClosingBalance.Text != string.Empty)
            {
                decimal.TryParse(this.labClosingBalance.Text, out closingBalance);
            }
            this.labAvailableBalance.Text = (orgCreditLimit - closingBalance).ToString("0.00");
        }

        private List<TStatementInvoiceDT> GetStatementInvoices(string statementGuid)
        {
            List<TStatementInvoiceDT> list = new List<TStatementInvoiceDT>();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SQLConnString"]))
            {
                conn.Open();

                using (SqlCommand comm = new SqlCommand())
                {
                    comm.Connection = conn;
                    comm.CommandText = string.Format("SELECT [ID] ,[StatementGuid] ,[Guid] ,[OriginalInvoiceID] ,[OriginalInvoiceGuid] ,[OrgSid] ,[InvoiceDate] ,[Principal] ,[Interest] ,[Penalty] ,[LateInterestCharges] ,[state] ,[Note] ,[PrincipalPaid] ,[InterestPaid] ,[PenaltyPaid] ,[LateInterestChargesPaid]  FROM  [tStatementInvoice] WHERE [StatementGuid]= '{0}'", statementGuid);
                    using (SqlDataReader reader = comm.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            TStatementInvoiceDT invoiceDT = TStatementInvoiceDT.ConvertFrom(reader);
                            list.Add(invoiceDT);
                        }
                    }
                }
            }

            return list;
        }


        public string GetSysOptionValue(int Sid)
        {
            TsysoptionBN nbn = new TsysoptionBN(this.Page);
            TsysoptionDT ndt = nbn.Get(Sid);
            nbn.Close();
            nbn.Dispose();
            if (ndt != null)
            {
                return ndt.OptionValue;
            }
            return "";
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    string outputDir = HttpContext.Current.Server.MapPath("~/PDF/Statement");
        //    PDFHelper.CreatPdf(this.Request.Url.OriginalString, outputDir);
        //}
    }
}